<?php
/* opdracht 2 */
	function encodeerSimple($string){
		$coded = strrev($string);
		if($coded{2} === strtoupper($coded{2})) {
		   $coded{2} = strtolower($coded{2});
		}elseif($coded{2} === strtolower($coded{2})) {
		   $coded{2} = strtoupper($coded{2});
		}
		
		$lenght = strlen($coded);
		if($lenght & 1) {
			echo '';
		} else {
			$lenght = strlen($coded);
			$lenght = $lenght / 2;
			$rest = substr($coded, 0, $lenght);
			$coded = substr($coded, $lenght);
			$coded = $rest.'#'.$coded;
		}	
		return $coded;
	}
	
	function decodeerSimple($string){
		$coded = str_replace('#', '', $string);
		
		
		if($coded{2} === strtoupper($coded{2})) {
		   $coded{2} = strtolower($coded{2});
		}elseif($coded{2} === strtolower($coded{2})) {
		   $coded{2} = strtoupper($coded{2});
		}
		
		$coded = strrev($coded);
		
		
		
		
		return $coded;
	}
	
/* opdracht 3 */
	function getAll(){
		$q = "SELECT * FROM bestelling";
		$r = mysql_query($q);
		while($row = mysql_fetch_assoc($r)){
			$bestelling .= '<tr><td>'.$row['id'].'</td>
							<td>'.$row['producten'].'</td>
							<td>&euro;'.$row['totaal'].'</td>
							<td>
							<form method="post" action="'.$_SERVER['PHP_SELF'].'">
								<input type="submit" name="delete" value="delete" />
								<input type="hidden" name="id" value="'.$row['id'].'" />
							</form></td>
							<td><a href="update.php?id='.$row['id'].'">Wijzig</a></td>
							</tr>';	
		}
		
		return $bestelling;
	}
	function delete($id){
		require 'inc.db.php';
		if(mysql_query("DELETE FROM bestelling WHERE id='".$id."'")){
			return true;
		}	
	}
	
	function update($id){
		if(isset($_POST['productA'])){
			$producten .= 'ProductA: '.$_POST['aantalA'].', ';
			$totaal[] = $_POST['aantalA'] * 2;
		}
		if(isset($_POST['productB'])){
			$producten .= 'ProductB: '.$_POST['aantalB'].', ';
			$totaal[] = $_POST['aantalB'] * 5;
		}
		if(isset($_POST['productC'])){
			$producten .= 'ProductC: '.$_POST['aantalC'].', ';
			$totaal[] = $_POST['aantalC'] * 6;
		}
		if(isset($_POST['productD'])){
			$producten .= 'ProductD: '.$_POST['aantalD'].', ';
			$totaal[] = $_POST['aantalD'] * 7;
		}
		if(isset($_POST['productE'])){
			$producten .= 'ProductE: '.$_POST['aantalE'].', ';
			$totaal[] = $_POST['aantalE'] * 4;
		}
		
		$totaal_euro = array_sum($totaal);
		if(mysql_query("UPDATE bestelling SET producten='".$producten."', datum='".date('Y-m-d H:i:s')."', totaal='".$totaal_euro."' WHERE id='".$_POST['id']."'")){
			return true;
		}
	}
?>